package com.example.shiwuyouproject.ui.fragment.my.activity

import android.os.Bundle
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivitySettingBinding
import com.example.shiwuyouproject.ui.LoginActivity
import com.example.shiwuyouproject.ui.fragment.my.viewmodel.SettingViewModel
import com.example.shiwuyouproject.ui.my.bean.PersonalInformationBean
import com.example.shiwuyouproject.ui.pup.LogOutPopupView
import com.example.shiwuyouproject.utils.ActStartUtils
import com.example.shiwuyouproject.utils.SPUtils
import com.example.shiwuyouproject.utils.SpConstant
import com.lxj.xpopup.XPopup
import com.yechaoa.yutilskt.ToastUtil
import com.yechaoa.yutilskt.YUtils

/**
 * 设置
 */
class SettingActivity : BaseVmActivity<ActivitySettingBinding, SettingViewModel>() {
    override fun viewModelClass(): Class<SettingViewModel> = SettingViewModel::class.java

    override fun getViewBinding(): ActivitySettingBinding {
        return ActivitySettingBinding.inflate(layoutInflater)
    }

    override fun initView() {
        if (SPUtils.getInstance().getString(SpConstant.USER_INFO).isNotEmpty()) {
            mBinding.tvMobile.text = SPUtils.getInstance().myUserInfo.mobile
        }

    }

    private var searchable = ""
    override fun initData() {
        searchable = intent.getStringExtra("searchable").toString()
        mBinding.tvCheck.isSelected = searchable != "0"
    }

    override fun setListener() {
        mBinding.tvBack.setOnClickListener { finish() }
        mBinding.tvCheck.setOnClickListener {
            YUtils.showLoading(this, "状态更改中...")
            if (mBinding.tvCheck.isSelected) {
                mViewModel.preservationCard("0")
            } else {
                mViewModel.preservationCard("1")
            }
        }
        mBinding.tvAbout.setOnClickListener { //关于师无忧

        }
        mBinding.upDatePassWord.setOnClickListener { //修改密码
            val mBundle=Bundle()
            mBundle.putString("stTitle", "修改密码")
            ActStartUtils.startForAct(this, ChangePasswordActivity::class.java,mBundle,100)
        }
        mBinding.tvEixtOut.setOnClickListener { //退出登录
            XPopup.Builder(this)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .asCustom(LogOutPopupView(this, object :
                    LogOutPopupView.onClickListener {
                    override fun onCenter() {
                        SPUtils.getInstance().clear()
                        ToastUtil.show("退出登录成功！")
                        ActStartUtils.startActClear(this@SettingActivity, LoginActivity::class.java)
                        finish()
                    }
                }))
                .show()
        }
    }

    override fun observe() {
        mViewModel.getPostStatus.observe(this, androidx.lifecycle.Observer<Boolean>{
            YUtils.hideLoading()
            if (it) {
                mBinding.tvCheck.isSelected = !mBinding.tvCheck.isSelected
            }
        })

    }
}